package com.gitee.melin.bee.core.jdbc.dialect.mysql;

import com.gitee.melin.bee.core.jdbc.dialect.AbstractDBQuery;

public class MySqlQuery extends AbstractDBQuery {

    @Override
    public String tablesSql(String schemaName) {
        return "select TABLE_NAME AS `NAME`,TABLE_SCHEMA AS `Database`,TABLE_COMMENT AS COMMENT,TABLE_CATALOG AS `CATALOG`"
                + ",TABLE_TYPE AS `TYPE`,ENGINE AS `ENGINE`,CREATE_OPTIONS AS `OPTIONS`,TABLE_ROWS AS `ROWS`"
                + ",CREATE_TIME,UPDATE_TIME, DATA_LENGTH from information_schema.tables"
                + " where TABLE_SCHEMA = '"
                + schemaName
                + "'";
    }

    @Override
    public String columnsSql(String schemaName, String tableName) {
        return "select DISTINCT a.COLUMN_NAME,a.COLUMN_TYPE,a.COLUMN_COMMENT,a.COLUMN_KEY,b.ORDINAL_POSITION KEY_ORDER,a.EXTRA AS AUTO_INCREMENT\n" +
                ",a.COLUMN_DEFAULT,a.IS_NULLABLE,a.NUMERIC_PRECISION,a.NUMERIC_SCALE,a.CHARACTER_SET_NAME\n" +
                ",a.COLLATION_NAME,a.ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS a \n" +
                "left join information_schema.KEY_COLUMN_USAGE  b on a.TABLE_SCHEMA = b.TABLE_SCHEMA and a.TABLE_NAME = b.TABLE_NAME and a.COLUMN_NAME = b.COLUMN_NAME\n" +
                "where a.TABLE_SCHEMA = '"+ schemaName +"' and a.TABLE_NAME = '"+ tableName +"' " +
                "order by a.ORDINAL_POSITION";
    }

    @Override
    public String schemaName() {
        return "Database";
    }

    @Override
    public String columnType() {
        return "COLUMN_TYPE";
    }
}
